﻿using DG.Tweening;
using DG.Tweening.Core;
using UnityEngine;
using UnityEngine.UI;
using LuaInterface;

#if UNITY_EDITOR
using UnityEditor;
#endif


public class TweenSettings
{
    public static void SetDelay(Tweener tween, float delay)
    {
        tween.SetDelay(delay);
    }

    public static void OnStart(Tweener tween, LuaFunction callback)
    {
        DG.Tweening.TweenCallback __callback = DelegateFactory.CreateDelegate(typeof(DG.Tweening.TweenCallback), callback) as DG.Tweening.TweenCallback;

        tween.OnStart(() =>
            {
                __callback();
                if (callback != null)
                    callback.Dispose();
            }
        );
    }

    public static void OnUpdate(Tweener tween, LuaFunction callback)
    {
        DG.Tweening.TweenCallback __callback = DelegateFactory.CreateDelegate(typeof(DG.Tweening.TweenCallback), callback) as DG.Tweening.TweenCallback;
        tween.OnUpdate(() =>
            {
                __callback();
            }
        );
    }

    public static void OnComplete(Tweener tween, LuaFunction callback)
    {
        DG.Tweening.TweenCallback __callback = DelegateFactory.CreateDelegate(typeof(DG.Tweening.TweenCallback), callback) as DG.Tweening.TweenCallback;
        tween.OnComplete(() =>
            {
                __callback();
                if (callback != null)
                    callback.Dispose();
            }
        );
    }

    public static void OnStart_cs(Tweener tween, TweenCallback callback)
    {
        tween.OnStart(callback);
    }

    public static void OnUpdate_cs(Tweener tween, TweenCallback callback)
    {
        tween.OnUpdate(callback);
    }

    public static void OnComplete_cs(Tweener tween, TweenCallback callback)
    {
        tween.OnComplete(callback);
    }

    public static void SetEase(Tweener tween, Ease ease)
    {
        tween.SetEase(ease);
    }

    public static void SetLoops(Tweener tween, int loops, LoopType type)
    {
        tween.SetLoops(loops, type);
    }

    public static void SetLoops(Tweener tween, int loops)
    {
        tween.SetLoops(loops);
    }

    public static void SetLoops(Sequence seque, int loops)
    {
        seque.SetLoops(loops);
    }

    public static void Restart(Tweener tween, bool includeDelay)
    {
        tween.Restart(includeDelay);
    }


    public static void PlayBackwards(Tweener tween)
    {
        tween.PlayBackwards();
    }

    /// <summary>
    /// 重置初始化状态
    /// </summary>
    /// <param name="tween"></param>
    public static void Rewind(Tweener tween)
    {
        tween.Rewind();
    }

    public static void Pause(Tweener tween)
    {
        tween.Pause();
    }

    public static void Kill(Tweener tween)
    {
        tween.Kill();
    }

    public static bool IsPlaying(Tween tween)
    {
        return tween.IsPlaying();
    }

    public static bool IsComplete(Tween tween)
    {
        return tween.IsComplete();
    }

    /// <summary>
    /// 设置动画进度
    /// </summary>
    /// <param name="tween"></param>
    /// <param name="progress"></param>
    public static void SetProgress(Tween tween, float progress)
    {
        tween.fullPosition = progress;
    }

    public static float GetProgress(Tween tween)
    {
        return tween.fullPosition;
    }



    public static Tweener ToInt(DOSetter<int> set, int start, int end, float time)
    {
        DOGetter<int> get = () => { return start; };
        return DOTween.To(get, set, end, time);
    }

    public static Tweener ToFloat(DOSetter<float> set, float start, float end, float time)
    {
        return DOTween.To(set, start, end, time);
    }

    public static Tweener ToFloatText(Text txt, float start, float end, float time)
    {
        return DOTween.To((value) =>
        {
            if (txt)
            {
                if (value > 0.01)
                {
                    txt.text = Mathf.Ceil(value).ToString();
                }
                else
                {
                    txt.text = "";
                }
            }
        }, start, end, time);
    }

    public static Tweener ToFloatTimeScale(float start, float end, float time)
    {
        return DOTween.To((value) =>
        {
            Time.timeScale = value;
        }, start, end, time);
    }

    public static Tweener ToFloatMaterial(Material mat, string propertyName, float start, float end, float time)
    {
        return DOTween.To((value) =>
        {
            if (mat)
            {
                mat.SetFloat(propertyName, value);
            }
        }, start, end, time);
    }

    public static Tweener DoPath(Transform trans, Vector3[] path, float duration, int pathtype, int pathmode, float lookat, int easetype)
    {
        var t = trans.DOPath(path, duration, (PathType)pathtype, (PathMode)pathmode, 10).SetLookAt(lookat).SetEase((Ease)easetype).OnUpdate(() =>
        {
            var rotate = trans.localEulerAngles;
            rotate.z = 0;
            trans.localEulerAngles = rotate;
        });

        return t;
    }
}
